package com.tistory.commons.utils;


public class MemberLinkUtil {

	/**
	 * 이메일의 '@' 앞부분 추출
	 * @param email 이메일
	 * @return 이메일코드
	 */
	public static String encodeEmail(String email) {
		int p = email.indexOf("@");
		if(p < 0) {
			return email;
		}
		return email.substring(0, p);
	}
	
	/**
	 * 이메일을 이메일코드로 변환
	 * @param email 이메일
	 * @return 이메일코드
	 */
	//--------------------------------------------------
	// 이메일 코드
	// "01" - 직접입력
	// "02" - naver.com
	// "03" - hanmail.net
	// "04" - hotmail.com
	// "05" - nate.com
	// "06" - yahoo.co.kr
	// "07" - empas.com
	// "08" - dreamwiz.com
	// "09" - freechal.com
	// "10" - lycos.co.kr
	// "11" - korea.com
	// "12" - gmail.com
	// "13" - hanmir.com
	// "14" - paran.com
	//--------------------------------------------------
	public static String encodeEmailCd(String email) {
		if(StringUtil.isEmpty(email)) {
			return "01";
		}
		
		int p = email.indexOf("@");
		if(p < 0) {
			return "01";
		}
		
		String domain = email.substring(p + 1).trim();
		
		if(domain.equals("naver.com"))			return "02";
		else if(domain.equals("hanmail.net"))	return "03";
		else if(domain.equals("hotmail.com"))	return "04";
		else if(domain.equals("nate.com"))		return "05";
		else if(domain.equals("yahoo.co.kr"))	return "06";
		else if(domain.equals("empas.com"))		return "07";
		else if(domain.equals("dreamwiz.com"))	return "08";
		else if(domain.equals("freechal.com"))	return "09";
		else if(domain.equals("lycos.co.kr"))	return "10";
		else if(domain.equals("korea.com"))		return "11";
		else if(domain.equals("gmail.com"))		return "12";
		else if(domain.equals("hanmir.com"))	return "13";
		else if(domain.equals("paran.com"))		return "14";
		
		return "01";
	}
	
	/**
	 * 이메일의 '@' 뒷부분 추출
	 * @param email
	 * @return
	 */
	public static String encodeEmailHost(String email) {
		int p = email.indexOf("@");
		if(p < 0) {
			return "";
		}
		return email.substring(p + 1);
	}
	
	/**
	 * 11번가 이메일 정보에서 이메일 추출
	 * @param email 이메일의 '@' 앞부분
	 * @param emailCd 이메일 코드
	 * @param drctEmail 이메일의 '@' 뒷부분
	 * @return
	 */
	public static String decodeEmail(String email, String emailCd, String drctEmail) {
		if("02".equals(emailCd)) return email + "@naver.com";
		if("03".equals(emailCd)) return email + "@hanmail.net";
		if("04".equals(emailCd)) return email + "@hotmail.com";
		if("05".equals(emailCd)) return email + "@nate.com";
		if("06".equals(emailCd)) return email + "@yahoo.co.kr";
		if("07".equals(emailCd)) return email + "@empas.com";
		if("08".equals(emailCd)) return email + "@dreamwiz.com";
		if("09".equals(emailCd)) return email + "@freechal.com";
		if("10".equals(emailCd)) return email + "@lycos.co.kr";
		if("11".equals(emailCd)) return email + "@korea.com";
		if("12".equals(emailCd)) return email + "@gmail.com";
		if("13".equals(emailCd)) return email + "@hanmir.com";
		if("14".equals(emailCd)) return email + "@paran.com";
		return email + "@" + drctEmail;
	}
	
	public static String encodeWrlsOper(String wrlsOper) {
		
		if("OR02501".equals(wrlsOper)) 		return "01";
		else if("OR02502".equals(wrlsOper)) return "03";
		else if("OR02503".equals(wrlsOper)) return "02";

		return "01";
	}
	
	public static String decodeWrlsOper(String wrlsOper) {
		
		if("01".equals(wrlsOper))		return "OR02501";
		else if("02".equals(wrlsOper))	return "OR02503";
		else if("03".equals(wrlsOper))	return "OR02502";
		
		return "OR02501";
	}

	public static String toYN(String yn) {
		return "Y".equals(yn) ? "Y" : "N";
	}

	public static String toStYN(String intgCls) {
		return intgCls == null ? "N" : intgCls.substring(0, 1);
	}
	
	public static String toChYN(String intgCls) {
		return intgCls == null ? "N" : intgCls.substring(1, 2);
	}
	
	public static String toMoYN(String intgCls) {
		return intgCls == null ? "N" : intgCls.substring(2, 3);
	}
	
	public static String toMemCls(String resdno) {
		
		if(StringUtil.isEmpty(resdno) || resdno.length() != 13) {
			return "ME00101";
		}
		
		String n = resdno.substring(6, 7);
		
		return (n.equals("5") || n.equals("6") || n.equals("7") || n.equals("8")) ? "ME00102" : "ME00101";
	}
	
	public static String toSex(String resdno) {

		if(StringUtil.isEmpty(resdno) || resdno.length() != 13) {
			return "M";
		}
		
		String n = resdno.substring(6, 7);
		
		return (n.equals("1") || n.equals("3")) ? "M" : "F";
	}
	
	public static String toAge(String resdno) {
		
		if(StringUtil.isEmpty(resdno) || resdno.length() != 13) {
			return "0";
		}

		String n = resdno.substring(6, 7);
		
		String birthYear = (n.equals("1") || n.equals("2") ? "19" : "20") + resdno.substring(0, 2);
		String currentYear = DateUtil.getCurrentYear();
		int age = Integer.parseInt(currentYear) - Integer.parseInt(birthYear) + 1;
		
		return "" + age;
	}
	
	public static String toBirthDt(String resdno) {
		
		if(StringUtil.isEmpty(resdno) || resdno.length() != 13) {
			return "0";
		}

		String n = resdno.substring(6, 7);
		
		return (n.equals("1") || n.equals("2") ? "19" : "20") + resdno.substring(0, 6);
	}
}
